using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Threading;

namespace CustomWorkflowActivitiesTestApplication
{
	public sealed partial class TestsRealSynchronizationScope: SequentialWorkflowActivity
	{
        private int _instanceName;
        public int InstanceName
        {
            get
            {
                return _instanceName;
            }
            set
            {
                _instanceName = value;
            }
        }

		public TestsRealSynchronizationScope()
		{
			InitializeComponent();
		}

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            Console.WriteLine("before " + this.QualifiedName + ":" + this.InstanceName);
        }

        private void codeActivity2_ExecuteCode(object sender, EventArgs e)
        {
            Console.WriteLine("after " + this.QualifiedName + ":" + this.InstanceName);
        }

        private void codeActivity3_ExecuteCode(object sender, EventArgs e)
        {
            if (!Program.Current.Equals(""))
            {
                throw new Exception();
            }
            for (int i = 0; i < Program.LOOP_COUNT; i++)
            {
                Program.Current = this.WorkflowInstanceId + ":1:" + i;
                Console.WriteLine("executing " + this.QualifiedName + ":1:" + this.InstanceName);
                Thread.Sleep(50);
                if (!Program.Current.Equals(this.WorkflowInstanceId + ":1:" + i))
                {
                    throw new Exception();
                }
            }
            Program.Current = "";
        }

        private void codeActivity4_ExecuteCode(object sender, EventArgs e)
        {
            if (!Program.Current.Equals(""))
            {
                throw new Exception();
            }
            for (int i = 0; i < Program.LOOP_COUNT; i++)
            {
                Program.Current = this.WorkflowInstanceId + ":2:" + i;
                Console.WriteLine("executing " + this.QualifiedName + ":2:" + this.InstanceName);
                Thread.Sleep(50);
                if (!Program.Current.Equals(this.WorkflowInstanceId + ":2:" + i))
                {
                    throw new Exception();
                }
            }
            Program.Current = "";
        }

        private void codeActivity5_ExecuteCode(object sender, EventArgs e)
        {
            if (!Program.Current.Equals(""))
            {
                throw new Exception();
            }
            for (int i = 0; i < Program.LOOP_COUNT; i++)
            {
                Program.Current = this.WorkflowInstanceId + ":3:" + i;
                lock (Program.guard)
                {
                    Program.Count++;
                }
                Console.WriteLine("executing " + this.QualifiedName + ":3:" + this.InstanceName);
                Thread.Sleep(50);
                if (!Program.Current.Equals(this.WorkflowInstanceId + ":3:" + i))
                {
                    throw new Exception();
                }
                if (i == 5 && InstanceName == 3)
                {
                    Program.Current = "";
                    throw new Exception("expected");
                }
            }
            Program.Current = "";
        }
    }
}
